package com.shop.cloud.service.system.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.shop.cloud.common.mybatis.BaseService;
import com.shop.cloud.common.mybatis.MybatisDao;
import com.shop.cloud.dao.system.ExtSysResourceDao;
import com.shop.cloud.dao.system.ExtSysRoleDao;
import com.shop.cloud.dao.system.SysRoleDao;
import com.shop.cloud.domain.system.RoleRessources;
import com.shop.cloud.domain.system.SysRole;
import com.shop.cloud.service.system.ISysRoleService;

@Service("sysRoleService")
public class SysRoleService extends BaseService<SysRole> implements ISysRoleService{
    
    @Autowired
	private SysRoleDao sysRoleDao;
    @Autowired
	private ExtSysRoleDao extSysRoleDao;
    @Autowired
    private ExtSysResourceDao extSysResourceDao;

	@Override
	public MybatisDao<SysRole> getEntityDao() {
		return sysRoleDao;
	}
	
	/* 
	* <p>Title: insert2</p>
	* <p>Description: </p>
	* @param entity
	* @return
	* @see com.magic.cloud.service.dddd#insert2(com.magic.cloud.model.system.SysRole)
	*/ 
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public int insert2(final SysRole entity){
		sysRoleDao.insert2(entity);
		sysRoleDao.insert(entity);
		throw new IllegalArgumentException("参数异常");
	}
	
	/* 
	* <p>Title: insertUserRole</p>
	* <p>Description: </p>
	* @param params
	* @return
	* @see com.magic.cloud.service.dddd#insertUserRole(java.util.Map)
	*/ 
	@Override
	public int insertUserRole(Map<String, Object> params){
		return extSysRoleDao.insertUserRole(params);
	}
	
	/* 
	* <p>Title: removeUserRole</p>
	* <p>Description: </p>
	* @param params
	* @return
	* @see com.magic.cloud.service.dddd#removeUserRole(java.util.Map)
	*/ 
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public int removeUserRole(Map<String, Object> params){
		return extSysRoleDao.removeUserRole(params);
	}
	
	/* 
	* <p>Title: findUserRole</p>
	* <p>Description: </p>
	* @param userId
	* @return
	* @see com.magic.cloud.service.dddd#findUserRole(java.lang.String)
	*/ 
	@Override
	public List<String> findUserRole(Long userId){
		Map<String, Object> params=new  HashMap<String, Object>();
		params.put("userId", userId);
		return extSysRoleDao.findUserRole(params);
	}

	@Override
	public List<String> findRole(Long roleId){
		Map<String, Object> params=new  HashMap<String, Object>();
		params.put("roleId", roleId);
		return extSysRoleDao.findRole(params);
	}
	/* 
	* <p>Title: insert</p>
	* <p>Description: </p>
	* @param entity
	* @param moduleIds
	* @return
	* @see com.magic.cloud.service.dddd#insert(com.magic.cloud.model.system.SysRole, java.lang.String)
	*/ 
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public int insert(SysRole entity, String moduleIds) {
		sysRoleDao.insert(entity);
		Long roleId=entity.getId();
		if(StringUtils.isNotEmpty(moduleIds)){
			String[] moduleIds_=moduleIds.split("[,]");
			for (String moduleId : moduleIds_) {
				Map<String,Object> params=new HashMap<String, Object>();
				params.put("roleId", roleId);
				params.put("moduleId", moduleId);
				extSysRoleDao.insertRoleModule(params);
			}
		}
		return 1;
	}
	
	/* 
	* <p>Title: insertTwo</p>
	* <p>Description: </p>
	* @param resourceIds
	* @param roleId
	* @return
	* @see com.magic.cloud.service.dddd#insertTwo(java.lang.String, java.lang.String)
	*/ 
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public int insertTwo(String resourceIds,String roleId) {
		extSysRoleDao.removeByRoleId(Long.parseLong(roleId));
		if(StringUtils.isNotEmpty(resourceIds)){
			String[] resourceIds_=resourceIds.split("[,]");
			List<RoleRessources> paramsList=new ArrayList<RoleRessources>();
			for (String resourceId : resourceIds_) {
				RoleRessources params=new RoleRessources();
				params.setResourceId(Long.parseLong(resourceId));
				params.setRoleId(Long.parseLong(roleId));
				paramsList.add(params);
			}
			extSysResourceDao.insertRoleResourceBatch(paramsList);
		}
		return 1;
	}
	
	/* 
	* <p>Title: removeResourceIds</p>
	* <p>Description: </p>
	* @param removeResourceIds
	* @param roleId
	* @return
	* @see com.magic.cloud.service.dddd#removeResourceIds(java.lang.String, java.lang.String)
	*/ 
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public int removeResourceIds(String removeResourceIds,String roleId) {
		if(StringUtils.isNotEmpty(removeResourceIds)){
			Map<String,Object> params=new HashMap<String, Object>();
			params.put("resourceIds", removeResourceIds.split(","));
			params.put("roleId", roleId);
			return extSysResourceDao.removeResourceIds(params);
		}
		return 0;
	}

	/* 
	* <p>Title: list</p>
	* <p>Description: </p>
	* @return
	* @see com.magic.cloud.service.dddd#list()
	*/ 
	@Override
	public List<Map<String, Object>> list() {
		return extSysRoleDao.list();
	}
    
}